package com.ftg.learn.service.impl;

import com.ftg.learn.domain.StudentDomain;
import com.ftg.learn.mapper.StudentMapper;
import com.ftg.learn.service.StudentService;
import com.ftg.learn.util.PageImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 当前类做一个组件直接注入到springboot,作为业务层处理
 * 把自动找到实现类赋值给   studentMapper   IOC 控制反转
 *
 * @author
 * @date 2024/2/29
 */
@Service
public class StudentServiceImpl implements StudentService {

    @Resource
    StudentMapper studentMapper;

    @Override
    public PageImpl showAll(String name, String sex, Integer start, Integer limit) {
        List<StudentDomain> list = studentMapper.showAll(name, sex, (start - 1) * limit, limit);
        Integer total = studentMapper.count(name, sex);
        PageImpl page = new PageImpl();
        page.setPageNo(start);
        page.setPageSize(limit);
        page.setTotal(total);
        page.setRows(list);
        return page;
    }

    @Override
    public int insert(StudentDomain studentDomain) {
        //项目这段会有大量的处理  (也比较简单)
        if (Objects.isNull(studentDomain)) {
            return -1;
        }

        if (studentMapper.checkSno(studentDomain.getSno()) == null) {
            return studentMapper.insert(studentDomain);
        } else {
            return -2;
        }
    }

}
